﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Security.Principal;

namespace MillerApp.CommonLib.Win32
{
    /// <summary>
    /// 平台调用（托管语言到非托管语言的dll调用）
    /// 可参照：http://www.pinvoke.net/default.aspx
    /// </summary>
    public class Win32Helper
    {
        // For Windows Mobile, replace user32.dll with coredll.dll
        //[DllImport("user32.dll", SetLastError = true)]
        //static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        [DllImport("advapi32.dll", SetLastError = true)]
        public static extern bool LogonUser(
           string lpszUsername,
           string lpszDomain,
           string lpszPassword,
           int dwLogonType,
           int dwLogonProvider,
           out IntPtr phToken
           );

        [DllImport("kernel32.dll", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool CloseHandle(IntPtr hObject);


        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern bool AnimateWindow
            (
            IntPtr hwnd, // handle to window　　　　
            int dwTime, // duration of animation　　　　
            int dwFlags // animation type　　　　
            );

        [DllImport("user32")]
        public static extern bool AnimateWindow(IntPtr hwnd, int time, AnimateWindowFlags flags);

        /// <summary>
        ///  AnimateWindow的dwFlags参数定义
        /// </summary>
        public enum AnimateWindowFlags : uint
        {
            AW_HOR_POSITIVE = 0x00000001, // 从左到右打开窗口　　
            AW_HOR_NEGATIVE = 0x00000002, // 从右到左打开窗口　　
            AW_VER_POSITIVE = 0x00000004, // 从上到下打开窗口　　
            AW_VER_NEGATIVE = 0x00000008, // 从下到上打开窗口　　
            AW_CENTER = 0x00000010, //若使用了AW_HIDE标志，则使窗口向内重叠；若未使用AW_HIDE标志，则使窗口向外扩展。　　
            AW_HIDE = 0x00010000, //隐藏窗口，缺省则显示窗口。　　
            AW_ACTIVATE = 0x00020000, //激活窗口。在使用了AW_HIDE标志后不要使用这个标志。　　
            AW_SLIDE = 0x00040000, //使用滑动类型。缺省则为滚动动画类型。当使用AW_CENTER标志时，这个标志就被忽略。　　
            AW_BLEND = 0x00080000, //使用淡出效果。只有当hWnd为顶层窗口的时候才可以使用此标志。　
        }

        /// <summary>
        ///获取系统帐号身份
        /// </summary>
        /// <param name="User">用户名</param>
        /// <param name="Domain">用户所属域名(如没有域则是机器名)</param>
        /// <param name="Password">用户密码</param>
        /// <returns></returns>
        public static WindowsIdentity CreateIdentity(string user, string domain, string password)
        {
            // The Windows NT user token.
            IntPtr tokenHandle = new IntPtr(0);

            const int LOGON32_PROVIDER_DEFAULT = 0;
            const int LOGON32_LOGON_NEW_CREDENTIALS = 9;

            //const int LOGON32_PROVIDER_DEFAULT = 0;
            //const int LOGON32_LOGON_NETWORK = 2;

            tokenHandle = IntPtr.Zero;

            // Call LogonUser to obtain a handle to an access token.
            bool returnValue = LogonUser(user, domain, password,
                LOGON32_LOGON_NEW_CREDENTIALS, LOGON32_PROVIDER_DEFAULT,
                out tokenHandle);

            if (false == returnValue)
            {
                int ret = Marshal.GetLastWin32Error();
                throw new Exception("LogonUser failed with error code: " + ret);
            }
            //The WindowsIdentity class makes a new copy of the token.
            //It also handles calling CloseHandle for the copy.
            WindowsIdentity id = new WindowsIdentity(tokenHandle);
            CloseHandle(tokenHandle);
            return id;
        }
    }
}
